//
//  ITWKWebViewController.m
//  InduceTool
//
//  Created by 魏延龙 on 2020/8/3.
//  Copyright © 2020 魏延龙. All rights reserved.
//

#import "ITWKWebViewController.h"
#import <WebKit/WebKit.h>
#import "ITCustomSchemeHandler.h"
#import "ITReplacingResourceURLProtocol.h"

@interface ITWKWebViewController ()

@property (nonatomic, strong) UIWebView *webView;
@property (nonatomic, strong) ITCustomSchemeHandler *schemeHandler;
@end

@implementation ITWKWebViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    // 一种方式
    // [NSURLProtocol registerClass:[ITReplacingResourceURLProtocol class]];
    // 另一种方式
    self.schemeHandler = [ITCustomSchemeHandler new];
    
    [self.view addSubview:self.webView];
}

-(void)viewWillLayoutSubviews {
    _webView.frame = self.view.bounds;
}

-(UIWebView *)webView {
    if (!_webView) {
        
        // 添加自适应屏幕宽度js调用的方法
//        NSString *jSString = @"var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=device-width'); document.getElementsByTagName('head')[0].appendChild(meta);";
//        WKUserScript *wkUserScript = [[WKUserScript alloc] initWithSource:jSString injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
//        WKUserContentController *userContentController = [WKUserContentController new];
//        [userContentController addUserScript:wkUserScript];
//
//        WKWebViewConfiguration* wkConfig = [[WKWebViewConfiguration alloc] init];
//        [wkConfig setURLSchemeHandler:self.schemeHandler forURLScheme:@"bltest"];
//        wkConfig.userContentController = userContentController;
        
//        _webView = [[WKWebView alloc] initWithFrame:CGRectZero configuration:wkConfig];
//        [self.view addSubview:_webView];
//
//        NSURL *url = [[NSBundle mainBundle] URLForResource:@"indexchange.html" withExtension:nil];
//        [self.webView loadRequest:[NSURLRequest requestWithURL:url]];
        
        _webView = [[UIWebView alloc] init];
        [_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://192.165.3.188:8080/mobile.html#/play-audio"]]];
        
    }
    return _webView;
}


@end
